import pymysql

from conn_mysql.conn_mysql import ConnectMysql

class ApiDetail(ConnectMysql):

    def open_id(self,openid):
        '''Look up all the information in the openid table,
        parameter: the user corresponds to openid'''

        sql = 'select * from openid where openid = %s'

        result_info = self.select_one(sql, (openid))

        return result_info

    def company_info(self, company_id):
        '''Present the data in the company table'''

        sql = 'select * from company where id = %s'

        result_info = self.select_one(sql, (company_id))

        return result_info

    
    def detail(self, methods, ID, openid=None):
        '''Single data, openid corresponds to pay judgment'''

        if not methods == 'GET':
            
                return {
                    'status': -1,
                    'errorCode': 4,
                    'errorMsg':f'请检查request.method:{methods}请重新请求',
                    'resultBody':None
                }

        if ID ==None:

            return {
                    'status': -1,
                    'errorCode': 3,
                    'errorMsg':f'请检查ID:{ID}请重新请求',
                    'resultBody':None
                }
        
        else:

            company_info = self.company_info(ID)

            if not company_info:

                return {
                    'status': -1,
                    'errorCode': 3,
                    'errorMsg':f'请检查ID:{ID}请重新请求',
                    'resultBody':None
                }

            else:

                company_info.pop('update_time')

                company_info.pop('regedate')

                company_info.pop('id')

                if openid ==None:
            
                    phone = company_info['phone'][0:3]+'****'+company_info['phone'][7:]

                    company_info['phone'] = phone

                    return {

                        'status': 1,
                        'errorCode': None,
                        'errorMsg':None,
                        'resultBody': company_info,
                    }
                
                else:

                    pay_state = self.open_id(openid)['pay']

                    if pay_state:

                        if pay_state == 1:

                            return {

                                'status': 1,
                                'errorCode': None,
                                'errorMsg':None,
                                'resultBody': company_info,
                            }

                        else:

                            phone = company_info['phone'][0:3]+'****'+company_info['phone'][7:]

                            company_info['phone'] = phone

                            return {

                                'status': 1,
                                'errorCode': None,
                                'errorMsg':None,
                                'resultBody': company_info,
                            }
                            
                    else:
            
                        phone = company_info['phone'][0:3]+'****'+company_info['phone'][7:]

                        company_info['phone'] = phone

                        return {
                            'status': 1,
                            'errorCode': None,
                            'errorMsg':None,
                            'resultBody':company_info
                        }